package com.gok.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * Created by mac on 2017/9/10.
 */
public class HttpUtils {
    private static CloseableHttpClient httpClient;
    private static PoolingHttpClientConnectionManager cm;

    public static void init(){
        int maxConn = 5;
        cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(maxConn);
        cm.setDefaultMaxPerRoute(maxConn);
        RequestConfig config = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).build();
        httpClient = HttpClients.custom().setConnectionManager(cm).setDefaultRequestConfig(config).build();
    }

    public static String post(String requestJson, String url) {
        try {
            if (httpClient == null) {
                init();
            }
            CloseableHttpResponse response = null;
            try {
                HttpPost post = new HttpPost(url);
                post.setEntity(new StringEntity(requestJson, ContentType.APPLICATION_JSON));
                response = httpClient.execute(post);
                String responseJson = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
                return responseJson;
            } finally {
                if (response != null) {
                    response.close();
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    public static String get(String url) {
        try {
            if (httpClient == null) {
                init();
            }
            CloseableHttpResponse response = null;
            try {
                HttpGet get = new HttpGet(url);
                response = httpClient.execute(get);
                String responseJson = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
                return responseJson;

            } finally {
                if (response != null) {
                    response.close();
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
